﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Framework.Controls;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;

using ESRI.ArcGIS.Carto;

using PlanningGIS.ArcGIS;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.Cartographic
{
    //自定义范围打印窗体
    public partial class FrmCustomPrint : Office2007Form
    {
        private static FrmCustomPrint instance = null;

        private IMapControl3 m_MapControl; //地图控件
        private IEnvelope m_pPrintArea; //打印范围
        private double dScale; //制图比例尺
        private int intPageID; //页面大小参数
        private short intOri; //页面方向

        public IMapControl3 MapControlOutProp
        {
            get { return m_MapControl; }
            set { m_MapControl = value; }
        }

        public IEnvelope PrintAreaProp
        {
            get { return m_pPrintArea; }
            set { m_pPrintArea = value; }
        }

        public double ScaleProp
        {
            get { return dScale; }
            set { dScale = value; }
        }

        public int PageIDProp
        {
            get { return intPageID; }
            set { intPageID = value; }
        }

        public short OriProp
        {
            get { return intOri; }
            set { intOri = value; }
        }

        private FrmCustomPrint()
        {
            InitializeComponent();
            //ZoomIn
            ButtonItem ZoomIn = new ButtonItem();
            ZoomIn.Tooltip = "放大";
            ControlsPageZoomInToolClass clsZoomIn = new ControlsPageZoomInToolClass();
            if (clsZoomIn.Bitmap != 0)
            {
                Bitmap bmpZoomIn = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsZoomIn.Bitmap);
                bmpZoomIn.MakeTransparent(bmpZoomIn.GetPixel(0, 0));
                ZoomIn.Image = bmpZoomIn;
            }
            ZoomIn.Click += delegate
            {
                clsZoomIn.OnCreate(this.axPageLayoutFrame.Object);
                clsZoomIn.OnClick();
                this.axPageLayoutFrame.CurrentTool = clsZoomIn;
            };
            bar2.Items.Add(ZoomIn);
            //ZoomOut
            ButtonItem ZoomOut = new ButtonItem();
            ZoomOut.Tooltip = "缩小";
            ControlsPageZoomOutToolClass clsZoomOut = new ControlsPageZoomOutToolClass();
            if (clsZoomOut.Bitmap != 0)
            {
                Bitmap bmpZoomOut = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsZoomOut.Bitmap);
                bmpZoomOut.MakeTransparent(bmpZoomOut.GetPixel(0, 0));
                ZoomOut.Image = bmpZoomOut;
            }
            ZoomOut.Click += delegate
            {
                clsZoomOut.OnCreate(this.axPageLayoutFrame.Object);
                clsZoomOut.OnClick();
                this.axPageLayoutFrame.CurrentTool = clsZoomOut;
            };
            bar2.Items.Add(ZoomOut);
            //Pan
            ButtonItem Pan = new ButtonItem();
            Pan.Tooltip = "平移";
            ControlsPagePanToolClass clsPan = new ControlsPagePanToolClass();
            if (clsPan.Bitmap != 0)
            {
                Bitmap bmpPan = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsPan.Bitmap);
                bmpPan.MakeTransparent(bmpPan.GetPixel(0, 0));
                Pan.Image = bmpPan;
            }
            Pan.Click += delegate
            {
                clsPan.OnCreate(this.axPageLayoutFrame.Object);
                clsPan.OnClick();
                this.axPageLayoutFrame.CurrentTool = clsPan;
            };
            bar2.Items.Add(Pan);
            //ZoomInFixed
            ButtonItem ZoomInFixed = new ButtonItem();
            ZoomInFixed.Tooltip = "固定放大";
            ControlsPageZoomInFixedCommandClass clsZoomInFixed = new ControlsPageZoomInFixedCommandClass();
            if (clsZoomInFixed.Bitmap != 0)
            {
                Bitmap bmpZoomInFixed = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsZoomInFixed.Bitmap);
                bmpZoomInFixed.MakeTransparent(bmpZoomInFixed.GetPixel(0, 0));
                ZoomInFixed.Image = bmpZoomInFixed;
            }
            ZoomInFixed.Click += delegate
            {
                clsZoomInFixed.OnCreate(this.axPageLayoutFrame.Object);
                clsZoomInFixed.OnClick();
            };
            bar2.Items.Add(ZoomInFixed);
            //ZoomOutFixed
            ButtonItem ZoomOutFixed = new ButtonItem();
            ZoomOutFixed.Tooltip = "固定缩小";
            ControlsPageZoomOutFixedCommandClass clsZoomOutFixed = new ControlsPageZoomOutFixedCommandClass();
            if (clsZoomOutFixed.Bitmap != 0)
            {
                Bitmap bmpZoomOutFixed = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsZoomOutFixed.Bitmap);
                bmpZoomOutFixed.MakeTransparent(bmpZoomOutFixed.GetPixel(0, 0));
                ZoomOutFixed.Image = bmpZoomOutFixed;
            }
            ZoomOutFixed.Click += delegate
            {
                clsZoomOutFixed.OnCreate(this.axPageLayoutFrame.Object);
                clsZoomOutFixed.OnClick();
            };
            bar2.Items.Add(ZoomOutFixed);
            //ZoomWholePage
            ButtonItem ZoomWholePage = new ButtonItem();
            ZoomWholePage.Name = "ZoomWholePage";
            ZoomWholePage.Tooltip = "缩放至全页";
            ControlsPageZoomWholePageCommandClass clsZoomWholePage = new ControlsPageZoomWholePageCommandClass();
            ZoomWholePage.Tag = clsZoomWholePage;
            if (clsZoomWholePage.Bitmap != 0)
            {
                Bitmap bmpZoomWholePage = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsZoomWholePage.Bitmap);
                bmpZoomWholePage.MakeTransparent(bmpZoomWholePage.GetPixel(0, 0));
                ZoomWholePage.Image = bmpZoomWholePage;
            }
            ZoomWholePage.Click += delegate
            {
                clsZoomWholePage.OnCreate(this.axPageLayoutFrame.Object);
                clsZoomWholePage.OnClick();
            };
            bar2.Items.Add(ZoomWholePage);
            //Zoom100Percent
            ButtonItem Zoom100Percent = new ButtonItem();
            Zoom100Percent.Tooltip = "缩放至100%";
            ControlsPageZoomWholePageCommandClass clsZoom100Percent = new ControlsPageZoomWholePageCommandClass();
            if (clsZoom100Percent.Bitmap != 0)
            {
                Bitmap bmpZoom100Percent = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsZoom100Percent.Bitmap);
                bmpZoom100Percent.MakeTransparent(bmpZoom100Percent.GetPixel(0, 0));
                Zoom100Percent.Image = bmpZoom100Percent;
            }
            Zoom100Percent.Click += delegate
            {
                clsZoom100Percent.OnCreate(this.axPageLayoutFrame.Object);
                clsZoom100Percent.OnClick();
            };
            bar2.Items.Add(Zoom100Percent);
            //PageWidth
            ButtonItem PageWidth = new ButtonItem();
            PageWidth.Tooltip = "缩放至页宽";
            ControlsPageZoomPageWidthCommandClass clsPageWidth = new ControlsPageZoomPageWidthCommandClass();
            if (clsPageWidth.Bitmap != 0)
            {
                Bitmap bmpPageWidth = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsPageWidth.Bitmap);
                bmpPageWidth.MakeTransparent(bmpPageWidth.GetPixel(0, 0));
                PageWidth.Image = bmpPageWidth;
            }
            PageWidth.Click += delegate
            {
                clsPageWidth.OnCreate(this.axPageLayoutFrame.Object);
                clsPageWidth.OnClick();
            };
            bar2.Items.Add(PageWidth);
            //ZoomPageToLastExtent
            ButtonItem ZoomPageToLastExtent = new ButtonItem();
            ZoomPageToLastExtent.Tooltip = "前一视图";
            ControlsPageZoomPageToLastExtentBackCommandClass clsZoomPageToLastExtent = new ControlsPageZoomPageToLastExtentBackCommandClass();
            if (clsZoomPageToLastExtent.Bitmap != 0)
            {
                Bitmap bmpZoomPageToLastExtent = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsZoomPageToLastExtent.Bitmap);
                bmpZoomPageToLastExtent.MakeTransparent(bmpZoomPageToLastExtent.GetPixel(0, 0));
                ZoomPageToLastExtent.Image = bmpZoomPageToLastExtent;
            }
            ZoomPageToLastExtent.Click += delegate
            {
                clsZoomPageToLastExtent.OnCreate(this.axPageLayoutFrame.Object);
                clsZoomPageToLastExtent.OnClick();
            };
            bar2.Items.Add(ZoomPageToLastExtent);
            //ZoomToLastExtent
            ButtonItem ZoomToLastExtent = new ButtonItem();
            ZoomToLastExtent.Tooltip = "后一视图";
            ControlsPageZoomPageToLastExtentForwardCommandClass clsZoomToLastExtent = new ControlsPageZoomPageToLastExtentForwardCommandClass();
            if (clsZoomToLastExtent.Bitmap != 0)
            {
                Bitmap bmpZoomToLastExtent = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsZoomToLastExtent.Bitmap);
                bmpZoomToLastExtent.MakeTransparent(bmpZoomToLastExtent.GetPixel(0, 0));
                ZoomToLastExtent.Image = bmpZoomToLastExtent;
            }
            ZoomToLastExtent.Click += delegate
            {
                clsZoomToLastExtent.OnCreate(this.axPageLayoutFrame.Object);
                clsZoomToLastExtent.OnClick();
            };
            bar2.Items.Add(ZoomToLastExtent);
            //SelectTool
            ButtonItem SelectTool = new ButtonItem();
            SelectTool.Tooltip = "选择元素";
            ControlsSelectToolClass clsSelectTool = new ControlsSelectToolClass();
            if (clsSelectTool.Bitmap != 0)
            {
                Bitmap bmpSelectTool = System.Drawing.Bitmap.FromHbitmap((IntPtr)clsSelectTool.Bitmap);
                bmpSelectTool.MakeTransparent(bmpSelectTool.GetPixel(0, 0));
                SelectTool.Image = bmpSelectTool;
            }
            SelectTool.Click += delegate
            {
                clsSelectTool.OnCreate(this.axPageLayoutFrame.Object);
                clsSelectTool.OnClick();
                this.axPageLayoutFrame.CurrentTool = clsSelectTool;
            };
            bar2.Items.Add(SelectTool);
            //print
            ButtonItem Print = new ButtonItem();
            Print.Tooltip = "打印";
            Print.Text = "打印";
            Print.Click += new System.EventHandler(this.btnPrint_Click);
            bar2.Items.Add(Print);
            //PrintSet
            ButtonItem PrintSet = new ButtonItem();
            PrintSet.Tooltip = "打印设置";
            PrintSet.Text = "打印设置";
            PrintSet.Click += new System.EventHandler(this.btnPrintset_Click);
            bar2.Items.Add(PrintSet);

            this.ShowInTaskbar = false;
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.MinimizeBox = true;
            this.MaximizeBox = true;
        }

        public static FrmCustomPrint CreateInstance()
        {
            if (instance == null || instance.IsDisposed == true)
            {
                instance = new FrmCustomPrint();
            }
            return instance;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //弹出打印界面
            FrmPrint thisFrm = new FrmPrint();
            thisFrm.PageLayerOut = axPageLayoutFrame.Object as IPageLayoutControl2;
            thisFrm.ShowDialog();
        }

        private void btnPrintset_Click(object sender, EventArgs e)
        {
            //弹出打印设置界面
            FrmPrintSet thisFrm = new FrmPrintSet();
            thisFrm.LayerOut = axPageLayoutFrame.Object as IPageLayoutControl2;
            thisFrm.ShowDialog();
        }

        private void FrmCustomPrint_Load(object sender, EventArgs e)
        {
            //缩放至打印区域
            //PrintHelper.ZoomtoPrintArea(m_pPrintArea, m_MapControl);

            //根据自定义范围、比例尺、页面大小、方向等参数在PageLayout上生成地图框
            PrintHelper.DrawCustomMapFrame(m_MapControl, axPageLayoutFrame.Object as IPageLayoutControl2, m_pPrintArea, dScale, intPageID, intOri);

            ControlsPageZoomWholePageCommandClass clsZoomWholePage = new ControlsPageZoomWholePageCommandClass();
            clsZoomWholePage.OnCreate(this.axPageLayoutFrame.Object);
            clsZoomWholePage.OnClick();
        }
    }
}
